package com.dongge.aop;

import com.dongge.annotation.HasRole;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * Created by zhudong on 2016/8/5.
 */
@Aspect
@Component
public class UserAspect {
    /**
     *
     */
    @Around("@annotation(com.dongge.annotation.IsLogin)")
    public Object checkIsLogin(ProceedingJoinPoint proceedingJoinPoint)throws Throwable{
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            return proceedingJoinPoint.proceed();
        }
        return "login";
    }

    @Around("@annotation(com.dongge.annotation.HasRole) && @annotation(hasRole)")
    public Object checkHasRole(ProceedingJoinPoint proceedingJoinPoint, HasRole hasRole) throws Throwable{
        Subject subject = SecurityUtils.getSubject();
        if(subject.hasRole(hasRole.value())){
            return proceedingJoinPoint.proceed();
        }
        return "noPermission";
    }
}
